package com.example.service.impl;

import com.example.annotation.ShiroData;
import com.example.common.DozerMapper;
import com.example.demo.Role;
import com.example.demo.User;
import com.example.mapper.RoleMapper;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private DozerMapper mapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    public void selectUser(String name){
        PageHelper.startPage(1,10);
        List<User> users = userMapper.select(null);
        PageInfo<User> pageInfo = new PageInfo(users);
        System.out.println(pageInfo.getTotal());
        System.out.println("dfdf"+name);
    }

    @Override
    @Transactional(value = ShiroData.TRANSACTION_MANAGER)
    public void addUser(User user) {
       userMapper.insert(user);
        System.out.println("aa");
        //userMapper.insertSelective(user);
        Role role = new Role();
        role.setRoleName("dd");
        roleMapper.insertSelective(role);
        System.out.println("ss");
    }

    @Override
    @Transactional
    public void insertList(List<User> list) {
         userMapper.insertList(list);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public List<User> findByAll(User user) {
      /*  userMapper.selectAll();
        userMapper.selectByExample(null);
        userMapper.selectCount(null);
       // userMapper.selectOneByExample(null);
        userMapper.selectOne(null);
        userMapper.selectByRowBounds(null,null);*/
      userMapper.selectByUserId(13);
        return userMapper.select(user);
    }

    @Override
    public User findById(Integer id) {
        //userMapper.selectByUserName("zhangsan");
        User user = new User();
        user.setId(id);
        user.setUserName("zhangsan");
        User usera = userMapper.selectByPrimaryKey(user);
        Role r =new Role();
        r.setId(1);
        Role role = roleMapper.selectByPrimaryKey(r);
        return user;
    }

    @Override
    public void findExample(User user) {
        List<User> users = userMapper.selectByExample(null);
        System.out.println("dfd");

    }

    @Override
    public PageInfo<User> findByPage(User user) {
        PageHelper.startPage(2,10);
        List<User> userlist  = userMapper.select(null);
        PageInfo<User> pageInfo = new PageInfo(userlist);
        return pageInfo;
    }
}
